package com.credithc.fastdfs.monitor.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.csource.common.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.credithc.fastdfs.api.entity.FileType;
import com.credithc.fastdfs.api.entity.SystemArea;
import com.credithc.fastdfs.monitor.service.ParamService;
import com.credithc.framework.core.mybatis.page.PageController;
import com.credithc.framework.core.mybatis.page.PageDto;
import com.credithc.framework.core.utils.DataGrid;
import com.credithc.framework.core.utils.JsonResult;

/**
 * @ClassName: ParamController
 * @Description: 系统参数
 * @author zhaoc
 * @date 2015年5月21日
 * @time 下午7:02:32
 */
@Controller
@RequestMapping(value = "/param")
public class ParamController {
	private static final Logger log = LoggerFactory.getLogger(MonitorController.class);
	
	@Resource(name = "paramServiceImpl")
	private ParamService paramServiceImpl;
	
	@RequestMapping(value = "sysParam")
	public ModelAndView sysParam() {
		ModelAndView mv = new ModelAndView("param/sysParam");
		return mv;
	}
	
	/** 
	 * @Title: listFileManage 
	 * @Description: 系统参数列表
	 * @param 
	 * @return DataGrid
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "sysParamToJson")
	public DataGrid sysParamToJson(PageDto pageDto, SystemArea sys, @RequestParam("sort") String sort,
			@RequestParam("order") String order, HttpServletRequest request, HttpServletResponse response) {
		PageController pc = pageDto.createPage();
		pc.setSort(sort);
		pc.setOrder(order);
		return DataGrid.formateList(paramServiceImpl.listSysParamInfo(pc, sys));
	}
	
	/**
	 * @throws MyException  
	 * @Title: addSysParam 
	 * @Description: 新增系统参数
	 * @param 
	 * @return DataGrid
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "addSysParam", method = RequestMethod.POST)
	public JsonResult addSysParam(SystemArea sys, HttpServletRequest request) throws MyException {
		int o = paramServiceImpl.addSysParam(sys);
		log.info("添加系统参数 : " + o);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("系统参数添加成功!");
		return jr;
	}
	
	/** 
	 * @Title: initEditSysParam 
	 * @Description: 系统参数编辑初始化
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "initEditSysParam", method = RequestMethod.POST)
	public JsonResult initEditSysParam(@RequestParam("sysId") String sysId, HttpServletRequest request) throws MyException {
		SystemArea sys = paramServiceImpl.getSysParamBySysId(sysId);
		log.info("初始化编辑系统参数 : " + sys);
		JsonResult jr = new JsonResult();
		if(sys != null) {
			jr.setSuccess(true);
			jr.setObj(sys);
		} else {
			jr.setSuccess(false);
			jr.setMsg("系统参数未找到");
		}
		return jr;
	}
	
	/** 
	 * @Title: editSysParam 
	 * @Description: 修改系统参数
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "editSysParam", method = RequestMethod.POST)
	public JsonResult editSysParam(SystemArea sys, HttpServletRequest request) throws MyException {
		int o = paramServiceImpl.editSysParam(sys);
		log.info("修改系统参数 : " + o);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("系统参数修改成功!");
		return jr;
	}
	
	/** 
	 * @Title: delSysParam 
	 * @Description: 删除系统参数
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "delSysParam", method = RequestMethod.POST)
	public JsonResult delSysParam(@RequestParam("sysIds") List<String> sysIds, HttpServletRequest request) throws MyException {
		int o = paramServiceImpl.delSysParam(sysIds);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("系统参数删除成功!");
		return jr;
	}
	
	/************************以下文件参数**********************/
	
	@RequestMapping(value = "fileParam")
	public ModelAndView fileParam() {
		ModelAndView mv = new ModelAndView("param/fileParam");
		return mv;
	}
	
	/** 
	 * @Title: fileParamToJson 
	 * @Description: 文件参数列表
	 * @param 
	 * @return DataGrid
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "fileParamToJson")
	public DataGrid fileParamToJson(PageDto pageDto, FileType file, @RequestParam("sort") String sort,
			@RequestParam("order") String order, HttpServletRequest request, HttpServletResponse response) {
		PageController pc = pageDto.createPage();
		pc.setSort(sort);
		pc.setOrder(order);
		return DataGrid.formateList(paramServiceImpl.listFileParamInfo(pc, file));
	}
	
	/** 
	 * @Title: addFileParam 
	 * @Description: 添加文件参数
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "addFileParam", method = RequestMethod.POST)
	public JsonResult addFileParam(FileType file, HttpServletRequest request) throws MyException {
		int o = paramServiceImpl.addFileParam(file);
		log.info("添加文件参数 : " + o);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("文件参数添加成功!");
		return jr;
	}
	
	/** 
	 * @Title: initEditFileParam 
	 * @Description: 初始化文件参数
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "initEditFileParam", method = RequestMethod.POST)
	public JsonResult initEditFileParam(@RequestParam("typeId") String typeId, HttpServletRequest request) throws MyException {
		FileType file = paramServiceImpl.getFileParamByFileId(typeId);
		log.info("初始化编辑文件参数 : " + file);
		JsonResult jr = new JsonResult();
		if(file != null) {
			jr.setSuccess(true);
			jr.setObj(file);
		} else {
			jr.setSuccess(false);
			jr.setMsg("文件参数未找到");
		}
		return jr;
	}
	
	/** 
	 * @Title: editFileParam 
	 * @Description: 修改文件参数
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "editFileParam", method = RequestMethod.POST)
	public JsonResult editFileParam(FileType file, HttpServletRequest request) throws MyException {
		int o = paramServiceImpl.editFileParam(file);
		log.info("修改文件参数 : " + o);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("文件参数修改成功!");
		return jr;
	}
	
	/** 
	 * @Title: delFileParam 
	 * @Description: 删除文件参数
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "delFileParam", method = RequestMethod.POST)
	public JsonResult delFileParam(@RequestParam("typeIds") List<String> typeIds, HttpServletRequest request) throws MyException {
		int o = paramServiceImpl.delFileParam(typeIds);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("文件参数删除成功!");
		return jr;
	}
}
